package com.example.shushiyunsst.fragment.main.home.child.miner22

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.example.library_common.global.AppGlobals
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.getUserBalanceInfo
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.ParentAccountBean
import com.example.shushiyunsst.util.MD5Util
import com.example.wallet.db.UserInfoManager
import com.lxj.xpopup.core.CenterPopupView
import com.taobao.accs.ACCSManager.sendData

/**
 * 合成母币
 * */
class CompoundIconPop(context: Context) : CenterPopupView(context) {

    fun setData(item: ParentAccountBean?): CompoundIconPop {
        this.item = item
        return this
    }


    override fun getImplLayoutId(): Int {
        return R.layout.compound_icon_pop
    }

    private var item: ParentAccountBean? = null

    private lateinit var mPassWordValue: EditText

    private lateinit var mCountValue: EditText

    private lateinit var mMoneyLeftParentValue: TextView

    private lateinit var mMoneyLeftParentTitle: TextView

    private lateinit var mMoneyLeftValue: TextView

    private lateinit var mMoneyLeftTitle: TextView

    private lateinit var mSureTv: TextView

    private lateinit var mPassWordLayout: View

    private lateinit var mSettingLayout: View

    private lateinit var mPreBtn: View

    private lateinit var mMoneyEmpty:View

    private var showPassword = false

    var dataSend=false

    var mMoneyEmptyV=false

    @SuppressLint("SetTextI18n")
    override fun onCreate() {
        super.onCreate()
        mPreBtn = findViewById(R.id.mPreBtn)
        mPassWordValue = findViewById(R.id.mPassWordValue)
        mCountValue = findViewById(R.id.mCountValue)
        mSureTv = findViewById(R.id.mSureTv)
        mMoneyLeftParentValue = findViewById(R.id.mMoneyLeftParentValue)
        mMoneyLeftParentTitle = findViewById(R.id.mMoneyLeftParentTitle)
        mMoneyLeftValue = findViewById(R.id.mMoneyLeftValue)
        mMoneyLeftTitle = findViewById(R.id.mMoneyLeftTitle)
        mPassWordLayout = findViewById(R.id.mPassWordLayout)
        mSettingLayout = findViewById(R.id.mSettingLayout)
        mMoneyEmpty = findViewById(R.id.mMoneyEmpty)
        val accountName = item?.accountName ?: ""

        val wallet_cml = AppGlobals.getApplication().getString(R.string.wallet_cml)

        val wallet_arl = AppGlobals.getApplication().getString(R.string.wallet_arl)

        if (accountName.contains(wallet_cml)) {
            mMoneyLeftValue.text = item?.cmlBalanceMixAvailable.toString()
            mMoneyLeftTitle.text = "$wallet_cml 子币可合成数量："

//            mMoneyLeftParentTitle.text = "$accountName :"
            mMoneyLeftParentTitle.text = "${wallet_cml}母币可合成数量:"
            mMoneyLeftParentValue.text = item?.scmlBalanceMixAvailable.toString()

        } else if (accountName.contains(wallet_arl)) {
            mMoneyLeftValue.text = item?.arlBalance.toString()
            mMoneyLeftTitle.text = "$wallet_arl 余额："

            mMoneyLeftParentTitle.text = "$accountName :"
            mMoneyLeftParentValue.text = item?.cmlBalanceMixAvailable.toString()
        }


        mSettingLayout.visibility = View.VISIBLE
        mPassWordLayout.visibility = View.INVISIBLE
        mPreBtn.visibility = View.GONE

        mSureTv.setOnClickListener {
            val mCountValueStr = mCountValue.text.toString()
            if (mCountValueStr.isEmpty()) {
                Toast.makeText(context, "请输入合成数量", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (!showPassword) {
                showPassword = true
                mSettingLayout.visibility = View.INVISIBLE
                mPassWordLayout.visibility = View.VISIBLE
                mPreBtn.visibility = View.VISIBLE
            } else {
                onCheckPassWord()

            }
        }

        mPreBtn.setOnClickListener {
            if (showPassword) {
                showPassword = false
                mSettingLayout.visibility = View.VISIBLE
                mPassWordLayout.visibility = View.INVISIBLE
                mPreBtn.visibility = View.GONE
            }
        }

        mMoneyEmpty.setOnClickListener {
            mMoneyEmptyV=true
            dismiss()
        }

    }

    private fun onCheckPassWord() {
        val mPassWordValueStr = mPassWordValue.text.toString()
        val mD5Str = MD5Util.getMD5Str(mPassWordValueStr)
        val payPasswd = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo?.payPasswd
        if (mD5Str == payPasswd) {
            onSendData()
        }else{
            Toast.makeText(context,"密码错误",Toast.LENGTH_SHORT).show()
        }


    }

    private fun onSendData() {
        val mCountValueStr = mCountValue.text.toString()
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val maps = mapOf(
            "txNums" to mCountValueStr,
            "uid" to "${userInfo?.uid}"
        )

        RestClient.builder()
            .url(R.string.mixScml)
            .raw(JSON.toJSONString(maps))
            .success {
                dataSend=true
                Toast.makeText(context, "合成成功", Toast.LENGTH_SHORT).show()
                getUserBalanceInfo(AppGlobals.getApplication())
                dismiss()
            }.failure {

                Toast.makeText(context, "合成失败", Toast.LENGTH_SHORT).show()
            }.error { _, msg ->
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()

            }
            .build()
            .post()
    }

}